home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C ++ / Applications / FlyThrough 1.1.2 / src / Source / QD3D General Tools / CCameraMaker.cp < prev    next >
Encoding:
Text File  |  1996-06-26  |  2.9 KB  |  116 lines  |  [TEXT/CWIE]

  1. //
  2. //    CCameraMaker.cp
  3. //
  4. //    class CCameraMaker
  5. //    A class that constructs a QuickDraw 3D View Angle Aspect camera.
  6. //
  7. //    It's designed to make it easy to override camera placement
  8. //    before calling Get() (or Make()) the first time.
  9. //
  10. //    by James Jennings
  11. //    November 26, 1995
  12. //
  13.  
  14. #pragma once
  15.  
  16. #include "CCameraMaker.h"
  17.  
  18. CCameraMaker::CCameraMaker(const SDimension16 &frameSize)
  19. {    // Fill in the structure default values.
  20.     // These values come from Apple's "Start Here" demo.
  21.     TQ3Point3D                     from     = { 0.0, 0.0, 7.0 };
  22.     TQ3Point3D                     to         = { 0.0, 0.0, 0.0 };
  23.     TQ3Vector3D                 up         = { 0.0, 1.0, 0.0 };
  24.  
  25.     float                         fieldOfView = 1.0;
  26.     float                         hither         = 0.001;
  27.     float                         yon         = 1000;
  28.     
  29.     SetLocation(from);
  30.     SetPointOfInterest(to);
  31.     SetUpVector(up);
  32.  
  33.     mData.cameraData.range.hither= hither;
  34.     mData.cameraData.range.yon     = yon;
  35.  
  36.     mData.cameraData.viewPort.origin.x = -1.0;
  37.     mData.cameraData.viewPort.origin.y = 1.0;
  38.     mData.cameraData.viewPort.width = 2.0;
  39.     mData.cameraData.viewPort.height = 2.0;
  40.     
  41.     mData.fov                = fieldOfView;
  42.     mData.aspectRatioXToY    =
  43.         (float) (frameSize.width) / (float) (frameSize.height);
  44. }
  45.  
  46. void
  47. CCameraMaker::Make()
  48. {
  49.     mObject = ::Q3ViewAngleAspectCamera_New(&mData);
  50.     ThrowIfNil_(mObject);
  51. }
  52.  
  53.  
  54. void
  55. CCameraMaker::SetLocation(TQ3Point3D &from)
  56. {
  57.     mData.cameraData.placement.cameraLocation     = from;
  58. }
  59.  
  60. void
  61. CCameraMaker::SetLocation(float x, float y, float z)
  62. {
  63.     ::Q3Point3D_Set(&(mData.cameraData.placement.cameraLocation), x, y, z);
  64. }
  65.  
  66. void
  67. CCameraMaker::SetPointOfInterest(TQ3Point3D &to)
  68. {
  69.     mData.cameraData.placement.pointOfInterest     = to;
  70. }
  71.  
  72. void
  73. CCameraMaker::SetPointOfInterest(float x, float y, float z)
  74. {
  75.     ::Q3Point3D_Set(&(mData.cameraData.placement.pointOfInterest), x, y, z);
  76. }
  77.  
  78. void
  79. CCameraMaker::SetUpVector(TQ3Vector3D &up, Boolean orthogonalize)
  80. {
  81.     mData.cameraData.placement.upVector = up;
  82.     if (orthogonalize) Orthogonalize();
  83. }
  84.  
  85. void
  86. CCameraMaker::SetUpVector(float x, float y, float z, Boolean orthogonalize)
  87. {
  88.     ::Q3Vector3D_Set(&(mData.cameraData.placement.upVector), x, y, z);
  89.     if (orthogonalize) Orthogonalize();
  90. }
  91.  
  92. void
  93. CCameraMaker::Orthogonalize()
  94. {    // make the upVector orthogonal to the line of sight vector
  95.     // (Uses Gramm-Smit orthogonalization.)
  96.     
  97.     TQ3Vector3D lineOfSight;    // v = vector from camera to point of interest
  98.     ::Q3Point3D_Subtract(&(mData.cameraData.placement.pointOfInterest), 
  99.                          &(mData.cameraData.placement.cameraLocation), 
  100.                          &lineOfSight);
  101.     
  102.     TQ3Vector3D up = mData.cameraData.placement.upVector;    // u = up
  103.     
  104.     ::Q3Vector3D_Normalize(&lineOfSight, &lineOfSight);        // v = v/|v|
  105.     ::Q3Vector3D_Normalize(&up, &up);                        // u = u/|u|
  106.     
  107.     float dot = ::Q3Vector3D_Dot(&lineOfSight, &up);        // u.v = cos(angle between)
  108.     
  109.     ::Q3Vector3D_Scale(&lineOfSight, dot, &lineOfSight);    //
  110.     ::Q3Vector3D_Subtract(&up, &lineOfSight, &up);            // u = u - v * (u.v)
  111.     ThrowIf_(up.x==0 && up.y==0 && up.z==0);                // u is now perpendicular to v
  112.     
  113.     mData.cameraData.placement.upVector = up;
  114. }
  115.  
  116.